***Table A6 

use "$data/APcontractlabor_final1.dta", clear

*global targ "/Users/vidhya.srajan/Dropbox/APcontract_reducedform/output/outputJuly2023"
*global output "/Users/rchaure1/Dropbox/APcontract_reducedform (1)/output/outputJan2024/"

	tabulate year, generate (yr_)

	keep if statusunit==1

	generate treat00 = treat*yr_1
	generate treat01 = treat*yr_2
	generate treat02 = treat*yr_3
	generate treat03 = treat*yr_4
	generate treat04 = treat*yr_5
	generate treat05 = treat*yr_6
	generate treat06 = treat*yr_7
	generate treat07 = treat*yr_8

	generate zero = 0
	label var treat00 "1999-2000"
	label var treat01 "2000-2001"
	label var treat02 "2001-2002"
	label var treat03 "2002-2003"
	label var treat04 "2003-2004"
	label var treat05 "2004-2005"
	label var treat06 "2005-2006"
	label var treat07 "2006-2007"
	label var zero "2002-2003"

	gen regularwages_total = regularsalaries/regular_totmandays
	gen lrwages_total = ln(regularwages_total)
	gen realregularwages = regularwages_total *(wpiop2011/wpiop)
	gen lrealregularwages = ln( realregularwages) 
	gen regularwagespaid_total = regularsalaries/regularmandayspaid
	gen lrwagespaid_total = ln(regularwagespaid_total)
	gen realregularwagespaid_total = regularwagespaid_total *(wpiop2011/wpiop)
	gen lrealregularwagespaid_total = ln(realregularwagespaid_total)

	gen price = egen_exfactoryvalop1/egen_qtysold1
	gen lprice = ln(price)

	gen lcapital = lplantmachinery_grossclosing  
	egen stateyear = group(state year)
	egen stateind4digit = group(state ind4digit)

	label variable lcw "log [contract workers]"
	label variable cwratio "contract workers ratio"
	label variable lrw "log [regular workers]"
	label variable lcm_man "log [contract worker person-days]"
	label variable lrm_man "log [regular worker person-days]"
	label variable cratio_man "contract persondays ratio" 
	label variable posttreat "Post X Treat"

	gen contracttoregular_workers = contract_workers/regular_workers
	gen contracttoregular_mandays = contract_mandays/regularmandays 
	gen logcontracttoregular_workers = ln(contracttoregular_workers+1)
	gen logcontracttoregular_mandays = ln(contracttoregular_mandays+1)

	gen cw = exp(lncw)
	gen w = exp(lnw)
	gen rw = exp(lnrw)
	gen cwratio_new = cw/w

	drop lncw_w lnrw_w 

	foreach v of varlist  lcm_man cratio_man cwratio lcw lcm_total lcm_nonman contract_workers contract_mandays contract_totmandays cratio_total cratio_nonman workers regular_workers regular_totmandays regularmandays regularwages_total allrevenue lrw lw lrwages_total lrm_man lrm_nonman lwm_total lrm_total lnrw lncw lnw cwratio_new cw rw w {
	winsor2 `v', cuts(5 95)
	}

	gen age = year - yearinitialprod
	replace age = . if year< yearinitialprod
	replace age = . if yearinitialprod==0
	replace age = . if yearinitialprod<1900
	
*keep if major_extended==1
bysort state: gen w2000_temp = w if year==2000
bysort state: egen w2000 = max(w2000_temp) 

gen newweight = weight * w2000

**Our variables: lncw_w cwratio_new_w lcm_total_w cratio_total_w lnrw_w lnw_w lrm_total_w lwm_total_w
gen cm_total_w = exp(lcm_total_w)
gen rm_total_w = exp(lrm_total_w)
gen wm_man = exp(lwm_man)
gen wm_total = exp(lwm_total)
*collapse (mean) cratio_total_w cwratio_new_w post treat  (sum) cm_total_w rm_total_w factory cw w rw nva nfcf revenue_sales , by(state year)	
*collapse (mean) cratio_total_w cwratio_new_w post treat  (sum) cm_total_w rm_total_w factory cw w rw nva nfcf revenue_sales [w=weight] , by(state year)	
*collapse (mean) cratio_total_w cwratio_new_w post treat  (sum) cm_total_w rm_total_w factory cw w rw nva nfcf revenue_sales [w=factory] , by(state year)	
collapse (mean) cratio_man_w cwratio_w cratio_nonman_w cratio_total_w cwratio_new_w post treat  (sum) cm_total_w rm_total_w wm_man wm_total factory cw w rw nva nfcf revenue_sales gva gcf netincome [w=w2000] , by(state year)	

gen prop = cw/w
gen prop_mandays = cm_total_w/wm_total
gen statecode = state
merge statecode using "$data/Census_2001.dta", sort uniqus force 
gen time = 1 if year==2000
replace time = 2 if year==2001
replace time = 3 if year==2002
replace time = 4 if year==2003
replace time = 5 if year==2004
replace time = 6 if year==2005
replace time = 7 if year==2006
replace time = 8 if year==2007
xtset state time
*}


synth cwratio_new_w rw(1 2) illiterate_total(1 2) sc_percent(1 2) cultivators_total(1 2), trunit(28) trperiod(4) fig

gen lcw = ln(cw)
gen lrw = ln(rw)


mat list e(W_weights) 
matrix D = e(W_weights)

svmat D, names(reg)
preserve 
keep reg1 reg2
rename reg1 state
rename reg2 synthweight
drop if state==.
saveold "$data/temp/weight.dta", replace
restore 

***regressions at the state-year level 
drop reg1 reg2
capture drop _merge
merge m:1 state using "$data/temp/weight.dta" 
replace synthweight=1 if state==28
tab year, gen(yr_)
generate treat00 = treat*yr_1
generate treat01 = treat*yr_2
generate treat02 = treat*yr_3
generate treat03 = treat*yr_4
generate treat04 = treat*yr_5
generate treat05 = treat*yr_6
generate treat06 = treat*yr_7
generate treat07 = treat*yr_8
gen zero=0
reghdfe lcw  treat01 treat02 zero treat04 - treat07  [aw=synthweight] , absorb(state year state##c.year)












***regressions at the firm level 
use "$data/APcontractlabor_final1.dta", clear
capture drop _merge
merge m:1 state using "$data/temp/weight.dta" 
replace synthweight=1 if state==28


	tabulate year, generate (yr_)

	keep if statusunit==1

	generate treat00 = treat*yr_1
	generate treat01 = treat*yr_2
	generate treat02 = treat*yr_3
	generate treat03 = treat*yr_4
	generate treat04 = treat*yr_5
	generate treat05 = treat*yr_6
	generate treat06 = treat*yr_7
	generate treat07 = treat*yr_8

	generate zero = 0
	label var treat00 "1999-2000"
	label var treat01 "2000-2001"
	label var treat02 "2001-2002"
	label var treat03 "2002-2003"
	label var treat04 "2003-2004"
	label var treat05 "2004-2005"
	label var treat06 "2005-2006"
	label var treat07 "2006-2007"
	label var zero "2002-2003"

	gen regularwages_total = regularsalaries/regular_totmandays
	gen lrwages_total = ln(regularwages_total)
	gen realregularwages = regularwages_total *(wpiop2011/wpiop)
	gen lrealregularwages = ln( realregularwages) 
	gen regularwagespaid_total = regularsalaries/regularmandayspaid
	gen lrwagespaid_total = ln(regularwagespaid_total)
	gen realregularwagespaid_total = regularwagespaid_total *(wpiop2011/wpiop)
	gen lrealregularwagespaid_total = ln(realregularwagespaid_total)

	gen price = egen_exfactoryvalop1/egen_qtysold1
	gen lprice = ln(price)

	gen lcapital = lplantmachinery_grossclosing  
	egen stateyear = group(state year)
	egen stateind4digit = group(state ind4digit)

	label variable lcw "log [contract workers]"
	label variable cwratio "contract workers ratio"
	label variable lrw "log [regular workers]"
	label variable lcm_man "log [contract worker person-days]"
	label variable lrm_man "log [regular worker person-days]"
	label variable cratio_man "contract persondays ratio" 
	label variable posttreat "Post X Treat"

	gen contracttoregular_workers = contract_workers/regular_workers
	gen contracttoregular_mandays = contract_mandays/regularmandays 
	gen logcontracttoregular_workers = ln(contracttoregular_workers+1)
	gen logcontracttoregular_mandays = ln(contracttoregular_mandays+1)

	gen cw = exp(lncw)
	gen w = exp(lnw)
	gen rw = exp(lnrw)
	gen cwratio_new = cw/w

	drop lncw_w lnrw_w 

	foreach v of varlist  lcm_man cratio_man cwratio lcw lcm_total lcm_nonman contract_workers contract_mandays contract_totmandays cratio_total cratio_nonman workers regular_workers regular_totmandays regularmandays regularwages_total allrevenue lrw lw lrwages_total lrm_man lrm_nonman lwm_total lrm_total lnrw lncw lnw cwratio_new cw rw w {
	winsor2 `v', cuts(1 99)
	}

	gen age = year - yearinitialprod
	replace age = . if year< yearinitialprod
	replace age = . if yearinitialprod==0
	replace age = . if yearinitialprod<1900

reghdfe cratio_total_w posttreat c.age##c.age if year<=2009 [pw=weight], cluster(state) absorb(factory year i.state#c.year i.ind3digit#i.year)
gen table2sample1 = 1 if e(sample)==1
reghdfe cwratio_new_w posttreat c.age##c.age if year<=2009 [pw=weight], cluster(state) absorb(factory year i.state#c.year i.ind3digit#i.year)
gen table2sample2 = 1 if e(sample)==1





est clear 
local i=1
foreach v of varlist  lncw_w cwratio_new_w lcm_total_w cratio_total_w {
reghdfe `v' posttreat c.age##c.age if year<=2009 & table2sample1==1 & table2sample2==1 [pw=synthweight], cluster(state) absorb(factory year i.state#c.year i.ind3digit#i.year)
gen sample`v'=e(sample)
eststo c`i'
estadd local factoryfe "Yes"
estadd local yearfe "Yes"
estadd local indyearfe "Yes"
estadd local statetrend "Yes"
qui estadd scalar rsq = e(r2)
esttab c`i'
local i=`i'+1
}

foreach v of varlist lnrw_w lnw_w lrm_total_w lwm_total_w  {
reghdfe `v' posttreat c.age##c.age if year<=2009 & table2sample1==1 & table2sample2==1  [pw=synthweight], cluster(state) absorb(factory year i.state#c.year i.ind3digit#i.year)
eststo c`i'
estadd local factoryfe "Yes"
estadd local yearfe "Yes"
estadd local indyearfe "Yes"
estadd local statetrend "Yes"
qui estadd scalar rsq = e(r2)
local i=`i'+1
}
		

# delimit ;
esttab  c1 c2 c3 c4 using "${output}/asi/TableA6.tex", replace
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Workers" "Worker-Days", pattern(1 0 1 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Log Contract" "Frac. Contract" "Log Contract" "Frac. Contract")
stats(rsq N, labels("R2" "N")fmt(3 0)) 
posthead("\midrule \multicolumn{4}{l}{\textit{\textbf{Panel A: Impact on Usage of Contract Workers}}} \\ \midrule")
postfoot("")
collabels(none) label booktabs nonotes
;
#delimit cr	


# delimit ;
esttab  c5 c6 c7 c8 using "${output}/asi/TableA6.tex", append
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel B: Impact on Payroll Workers and Firm Size}}} \\ \midrule")
mtitles("Log Regular" "Log Total" "Log Regular" "Log Total")
collabels(, none) nonumbers label booktabs nonotes
stats(rsq N, labels("R2" "N")fmt(3 0)) 
;
#delimit cr	
